package cn.zxhysy.booksmall.controller;

import cn.zxhysy.booksmall.utils.ApiJSONResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Description 上传文件接口
 */
@Api(value = "文件上传", tags = "上传文件接口")
@RestController
@RequestMapping(value = {"/upload", "/api/upload"})
public class UploadController {
    @Value("${filePath}")
    private String baseFilePath;

    @ApiOperation(value = "上传图片")
    @PostMapping("/image")
    public ApiJSONResult uploadImage(@RequestParam(value = "file") @ApiParam(value="文件对象", required = true)  MultipartFile file, HttpServletRequest request) throws IOException {
        Files.write(Paths.get(baseFilePath + File.separator + file.getOriginalFilename()), file.getBytes());
        return ApiJSONResult.ok( "/static/image/"+ file.getOriginalFilename());
    }
}
